package net.biz.template.directive;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;
import net.biz.Filter;
import net.biz.entity.ArticleCategory;
import net.biz.service.ArticleCategoryService;

@Component("hotMessageDirective")
public class HotMessageDirective  extends BaseDirective {

	/** 变量名称 */
	private static final String VARIABLE_NAME = "result";
	
	@Resource(name = "articleCategoryServiceImpl")
	private ArticleCategoryService articleCategoryService;
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException {
		Map<String, Object> result=new HashMap<String, Object>();
		List<ArticleCategory> list=null;
		boolean useCache = useCache(env, params);
		String cacheRegion = getCacheRegion(env, params);
		if (useCache) {
			list= articleCategoryService.findHotMess(String.valueOf(params.get("name")));
		} else {
			list = articleCategoryService.findHotMess(String.valueOf(params.get("name")),cacheRegion);
		}
		if(null!=list&&!list.isEmpty()){
			result.put("acc", list.get(0));
		}
		setLocalVariable(VARIABLE_NAME, result, env, body);
	}

}
